package com.caigua.mgp.p;

import com.caigua.mgp.mg.utils.GenUtil;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import com.intellij.ui.content.ContentManager;
import org.mybatis.generator.config.Configuration;

import javax.swing.*;
import java.util.List;

/**
 * @BelongsProject: toolWindow
 * @BelongsPackage: org.intellij.sdk.toolWindow
 * @Author: caigua
 * @CreateTime: 2020-12-07 14:27
 * @Description: todo
 */
public class MainWindow extends JFrame {

    private JPanel dbConnectPanel;
    private JList list1;
    private JTextArea textArea1;
    private JTextField connectionUrlTextField;
    private JLabel connectionUrlLabel;
    private JLabel passwordLabel;
    private JTextField passwordTextField;
    private JLabel usernameLabel;
    private JTextField usernameTextField;
    private JButton connectButton;
    private JButton testConnectButton;

    private OptionWindow optionWindow;

    public MainWindow(ToolWindow toolWindow) {
        connectButton.addActionListener(e -> {

            GenUtil.setDBInfo(connectionUrlTextField.getText(), usernameTextField.getText(), passwordTextField.getText());
            try {
                GenUtil.getTableInfo();
            } catch (Throwable throwable) {
                Messages.showMessageDialog(throwable.getMessage(), "错误信息", null);
            }
            optionWindow.loadTableList();
            Messages.showMessageDialog("连接成功", "信息提示", null);
            ContentManager contentManager = toolWindow.getContentManager();
            contentManager.setSelectedContent(contentManager.findContent("编辑"));

            /*Content mybatis_generator_code_plugin = toolWindow.getContentManager().findContent("mybatis generator code plugin");
            ContentManager contentManager = toolWindow.getContentManager();
            toolWindow.getContentManager().removeContent( mybatis_generator_code_plugin, true,true,true);
            ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
            OptionWindow optionWindow = new OptionWindow(toolWindow);
            Content content = contentFactory.createContent(optionWindow.getContentPanel(), "", false);
            toolWindow.getContentManager().addContent(content);*/
        });

        testConnectButton.addActionListener(e -> {
            GenUtil.setDBInfo(connectionUrlTextField.getText(), usernameTextField.getText(), passwordTextField.getText());
            try {
                GenUtil.testConnect();
                Messages.showInfoMessage("可以正常连接", "提示信息");
            } catch (Throwable t) {
                Messages.showErrorDialog(t.getMessage(), "错误信息");
            }
        });
    }

    public JPanel getDbConnectPanel() {
        return dbConnectPanel;
    }

    public OptionWindow getOptionWindow() {
        return optionWindow;
    }

    public void setOptionWindow(OptionWindow optionWindow) {
        this.optionWindow = optionWindow;
    }
}
